package com.example.essayController;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.w3c.dom.Document;

public abstract class Controller {
	
	protected static final String siteUri = "http://localhost:8080/web_rmp/resource/";
	protected static final String projectName = "essayclient/";
	private HttpClient httpClient;
	
	protected boolean execHttpPut(Document document, String uri) throws ClientProtocolException, IOException, TransformerException {
		httpClient = new DefaultHttpClient();
		try {
			TransformerFactory tFactory = TransformerFactory.newInstance();
			Transformer transformer = tFactory.newTransformer();
		    DOMSource dSource = new DOMSource(document);
			ByteArrayOutputStream outputStr = new ByteArrayOutputStream();
	        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
	        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
	        StreamResult streamRes = new StreamResult(new OutputStreamWriter(outputStr, "gb2312"));
	        transformer.transform(dSource, streamRes);
	        System.out.println(outputStr.toString());
	        
	        StringEntity resEntity = new StringEntity(outputStr.toString(), "UTF-8");
	        resEntity.setChunked(true);
	        resEntity.setContentType("text/xml");
	        resEntity.setContentEncoding("UTF-8");
			HttpPut httpPut = new HttpPut(uri);
			httpPut.setEntity(resEntity);
			System.out.println("executing request " + httpPut.getRequestLine());
			HttpResponse response = httpClient.execute(httpPut);
			System.out.println(response.getStatusLine());
		} finally {
			httpClient.getConnectionManager().shutdown();
		}
		return true;	        
	}
	
	protected boolean execHttpPost(Document document) throws TransformerException, ClientProtocolException, IOException {
		httpClient = new DefaultHttpClient();
		try {
			TransformerFactory tFactory = TransformerFactory.newInstance();
			Transformer transformer = tFactory.newTransformer();
			//Format ;
		    DOMSource dSource = new DOMSource(document);
			ByteArrayOutputStream outputStr = new ByteArrayOutputStream();
	        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
	        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
	        StreamResult streamRes = new StreamResult(new OutputStreamWriter(outputStr, "gb2312"));
	        transformer.transform(dSource, streamRes);
	        System.out.println(outputStr.toString());

	        StringEntity resEntity = new StringEntity(outputStr.toString(), "UTF-8");
	        resEntity.setChunked(true);
	        resEntity.setContentType("text/xml");
	        resEntity.setContentEncoding("UTF-8");
	        
			// Interesting: "Essay/*" and "User*/" have the same effect, 
	        // 				that's why I can put it in Controller,
	        //				and don't need uri
			HttpPost httpPost = new HttpPost(siteUri + projectName + "essay/*");
			httpPost.setEntity(resEntity);
			System.out.println("executing request " + httpPost.getRequestLine());
			HttpResponse response = httpClient.execute(httpPost);
			System.out.println(response.getStatusLine());
		} finally {
			httpClient.getConnectionManager().shutdown();
		}
		return true;
	}
	
	protected boolean execHttpDelete(String uri) throws ClientProtocolException, IOException {
		httpClient = new DefaultHttpClient();
		try {
			httpClient = new DefaultHttpClient();
			HttpDelete httpDelete = new HttpDelete(uri);	
			HttpResponse response = httpClient.execute(httpDelete);
			System.out.println(response.getStatusLine());
		} finally {
			httpClient.getConnectionManager().shutdown();
		}
		return true;
	}
}
